﻿
using System;
using SqlSugar;
using System.Threading.Tasks;
using Dora.Orm.SqlSugarCore.UnitOfWork;
using Surging.Core.Common;
using System.Collections.Generic;
using Dora.Services.AdminUser.Entities;
using Dora.Services.AdminUser.Dto.QueryDto;
using Dora.Services.AdminUser.Dto.ViewDto;
using Dora.Tools.Enums.AdminUser;

namespace Dora.Services.AdminUser.Repository.Repositorys
{
    /// <summary>
    /// sms记录表仓储层
    /// </summary>
    public class SmsRecordRepository : SqlSugarRepository<SmsRecordEntity, Guid>
    {
        private ISugarUnitOfWork _unitOfWork;
        public SmsRecordRepository(ISugarUnitOfWork unitOfWork) : base(unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        /// <summary>
        /// 分页获取数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageDataList<SmsRecordViewDto>> GetPageList(SmsRecordQueryDto input)
        {
            var pagePredict = _unitOfWork.QueryClient.Queryable<SmsRecordEntity>()
                .Where(it => it.IsDeleted == false)
                .WhereIF(!string.IsNullOrEmpty(input.SystemKey), it => it.SystemKey == input.SystemKey.Trim())
                .WhereIF(input.TopOrgId.HasValue, it => it.TopOrgId == input.TopOrgId)
                .WhereIF(input.OrgId.HasValue, it => it.OrgId == input.OrgId)
                .WhereIF(!string.IsNullOrEmpty(input.SendName), it => it.SendName.Contains(input.SendName.Trim()))
                .WhereIF(!string.IsNullOrEmpty(input.Mobile), it => it.Mobile.Contains(input.Mobile.Trim()))
                .WhereIF(!string.IsNullOrEmpty(input.Keywords), it => it.SmsContent.Contains(input.Keywords.Trim()))
                .WhereIF(input.SendTimeStart.HasValue, it => it.SendTime >= input.SendTimeStart)
                .WhereIF(input.SendTimeEnd.HasValue, it => it.SendTime < input.SendTimeEnd.Value.AddDays(1))
                ;
            var totalCount = await pagePredict.CountAsync();
            var pageList = await pagePredict.OrderBy(it => it.Id, OrderByType.Desc)
               .Select(it => new SmsRecordViewDto() {
                   SendName=it.SendName,
                   Mobile=it.Mobile,
                   SendTime=it.SendTime,
                   SmsContent=it.SmsContent
               })
                .ToPageListAsync(input.PageIndex, input.PageSize);
            var page = new PageDataList<SmsRecordViewDto>(totalCount, pageList, input.PageIndex, input.PageSize);
            return page;
        }

        /// <summary>
        /// 分页获取组织机构统计短信发送数量数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageDataList<SmsRecordOrgStaticsViewDto>> GetOrgStaticsPageList(SmsRecordOrgStaticsQueryDto input)
        {
            var pagePredict = _unitOfWork.QueryClient.Queryable<OrganizationEntity,SmsRecordEntity>((o,sr)=>new object[] {
                JoinType.Inner,o.Id==sr.OrgId
            })
                .Where((o, sr) => o.IsDeleted == 0&&sr.IsDeleted == false)
                .WhereIF(!string.IsNullOrEmpty(input.SystemKey), (o, sr) => sr.SystemKey == input.SystemKey)
                .WhereIF(input.TopOrgId.HasValue, (o, sr) => sr.TopOrgId == input.TopOrgId)
                .WhereIF(!string.IsNullOrEmpty(input.OrgLevelPath), (o, sr) => o.LevelPath.StartsWith( input.OrgLevelPath))
                .WhereIF(input.POrgType.HasValue && input.POrgType == PermissionOrgType.BigTravelAgency, (o, sr) => o.OrgType == (int)OrgType.General && o.OrgLevel == (int)OrgLevel.Master)
                .WhereIF(input.POrgType.HasValue && input.POrgType == PermissionOrgType.SubTravelAgency, (o, sr) => o.OrgType == (int)OrgType.Distributor && o.OrgLevel == (int)OrgLevel.Branch)
                .WhereIF(input.POrgType.HasValue && input.POrgType == PermissionOrgType.Shop, (o, sr) => (o.OrgType == (int)OrgType.Distributor && o.OrgLevel == (int)OrgLevel.Store) || (o.OrgType == (int)OrgType.DirectlyUnder && o.OrgLevel == (int)OrgLevel.DirectlyUnder))
                .WhereIF(!string.IsNullOrEmpty(input.OrgName), (o, sr) => o.Name.Contains(input.OrgName.Trim()))
                .WhereIF(!string.IsNullOrEmpty(input.LeaderName), (o, sr) => o.LeaderName.Contains(input.LeaderName.Trim()))
                .WhereIF(!string.IsNullOrEmpty(input.LeaderPhone), (o, sr) => o.LeaderPhone.Contains(input.LeaderPhone.Trim()))
                .GroupBy((o, sr) =>o.Id)
                ;
            var totalCount = await pagePredict.CountAsync();
            var pageList = await pagePredict.OrderBy((o, sr) => o.Id, OrderByType.Asc)
                .Select((o, sr)=>new SmsRecordOrgStaticsViewDto {
                    OrgId=o.Id,
                    OrgName=o.Name,
                    OLevel = o.OrgLevel,
                    OType = o.OrgType,
                    SmsCount=SqlFunc.AggregateCount(sr.Id)
                })
                .ToPageListAsync(input.PageIndex, input.PageSize);
            var page = new PageDataList<SmsRecordOrgStaticsViewDto>(totalCount, pageList, input.PageIndex, input.PageSize);
            return page;
        }
    }
}
